package com.huayal.mall.product.service.impl;

import com.huayal.mall.product.dao.AttrAttrgroupRelationDao;
import com.huayal.mall.product.entity.AttrAttrgroupRelationEntity;
import com.huayal.mall.product.entity.AttrEntity;
import com.huayal.mall.product.service.AttrAttrgroupRelationService;
import com.huayal.mall.product.service.AttrService;
import com.huayal.mall.product.vo.AttrGroupRelationVo;
import com.huayal.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huayal.common.utils.PageUtils;
import com.huayal.common.utils.Query;

import com.huayal.mall.product.dao.AttrGroupDao;
import com.huayal.mall.product.entity.AttrGroupEntity;
import com.huayal.mall.product.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Resource
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        Object key = params.get("key");
        QueryWrapper<AttrGroupEntity> attrGroupEntityQueryWrapper = new QueryWrapper<>();
        if (key!=null&&!"".equals(key)){
            attrGroupEntityQueryWrapper.eq("attr_group_id",key).or().like("attr_group_name",key);
        }

        if (catelogId==0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    attrGroupEntityQueryWrapper
            );
            return new PageUtils(page);
        }

        attrGroupEntityQueryWrapper.eq("catelog_id",catelogId);
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                attrGroupEntityQueryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getAttrListByAttrGroupId(Long attrGroupId) {

        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.getBaseMapper().selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id",attrGroupId));
        List<AttrEntity> collect = list.stream()
                .map(entity -> attrService.getById(entity.getAttrId()))
                .filter(attrEntity -> attrEntity != null)
                .collect(Collectors.toList());


        return collect;
    }

    @Override
    public void deleteAttrGroupRelation(AttrGroupRelationVo[] vos) {

        List<AttrAttrgroupRelationEntity> list = Arrays.stream(vos).map(AttrGroupRelationVo -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(AttrGroupRelationVo, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());

        attrAttrgroupRelationDao.deleteBatchAttrGroupRelation(list);
    }

    @Override
    public void addAttrRelation(List<AttrGroupRelationVo> vos) {

        attrAttrgroupRelationService.addAttrRelation(vos);
    }

    /**
     * 1，根据对应的三级编号查询对应的所有属性组
     * 2，根据属性组查询到对应的属性信息
     * @param catalogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrgroupWithAttrsByCatalogId(Long catalogId) {

        //1，根据对应的三级编号查询对应的所有属性组
        List<AttrGroupEntity> attrGroups = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catalogId));
        //2，根据属性组查询到对应的属性信息
        List<AttrGroupWithAttrsVo> list = attrGroups.stream().map(group -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,vo);
            //根据属性组找到所有的属性信息
            vo.setAttrs(attrService.getRelationAttr(group.getAttrGroupId()));
            return vo;
        }).collect(Collectors.toList());

        return list;
    }


}